/**
 * Created by Lwang on 2017-03-30.
 * 注册控制器
 */
import Util from "utils"
import $ from "jquery";


var registerCtrl = ['$scope', '$stateParams', '$state', '$location', '$http', '$timeout', '$rootScope', 'Http', function ($scope, $stateParams, $state, $location, $http, $timeout, $rootScope, Http) {
    var smsCode = "";
    var cutNum = 60;
    $scope.wrongShow = false;
    $scope.wrongTips = "";
    $scope.phone = "";
    $scope.smsCode = "";
    $scope.sendHtml = "发送验证码";
    $scope.isGetCode = true;
    $scope.cutDownNum = 60 + "秒";
    $scope.isChecked = true;
    $scope.protocolShow = false;
    $(".more-inside").css("height",$(window).height() - 50);

    Http.request({
        url: Util.ajax.isLogin,
        method: "get",
        success: function (res) {
            Http.handler(Util.merge(res, {
                success: function (data) {
                    if (!data.result) {
                        if ($state.current.name.indexOf("register") == -1) {
                            location.href = "http://m.jshrdtz.com/register";
                        }
                    } else {
                        location.replace("http://m.jshrdtz.com/myFinance");
                    }


                }
            }))
        }
    });


    //进行登录
    $scope.goToLogin = function () {
        if (!Util.checkMobile($scope.phone)) {
            Util.tips({
                content: "请填写正确的手机号码!!",
            });
            return;
        }
        if(!$scope.smsCode){
            Util.tips({
                content: "请填写验证码!!",
            });
            return;
        }
        if(!$scope.isChecked){
            Util.tips({
                content: "请先阅读用户注册服务协议!!",
            });
            return;
        }

        Http.request({
            url: Util.ajax.register + "?mobile=" + $scope.phone + "&smsCode=" + $scope.smsCode,
            method: "POST",
            success: function (res) {
                Http.handler(Util.merge(res, {
                    success: function (res) {
                        $state.go("myFinance");
                    },
                    exception: function (res) {
                    },
                    error: function (res) {
                    }
                }))
            }
        })
    };
//    获取验证码
    $scope.getCode = function () {
        if (!Util.checkMobile($scope.phone)) {
            Util.tips({
                content: "请填写正确的手机号码!!"
            });
            return;
        }
        cutDown(cutNum);

        Http.request({
            url: Util.ajax.smsToken,
            method: "GET",
            success: (res) => {
                Http.handler(Util.merge(res, {
                    success: function (res) {
                        var token = res.result;
                        smsCode = res.result;
                        Http.request({
                            url: Util.ajax.sendsms($scope.phone) + "?smsToken=" + token,
                            method: "GET",
                            success: (result) => {
                                Http.handler(Util.merge(result, {
                                    success: function (result) {

                                    }
                                }))
                            }
                        })
                    }
                }));

            },
            error: (err) => {
                console.log(err);
            }
        });
    };

    function cutDown(second, callback) {
        if (second == 0) {
            cutNum = 60;
            $scope.cutDownNum = 60 + "秒";
            $scope.sendHtml = "重新发送";
            $scope.isGetCode = true;
            Util.getType(callback) == "function" && callback();
        } else {
            $scope.isGetCode = false;
            $timeout(function () {
                cutNum--;
                $scope.cutDownNum = cutNum + "秒";
                cutDown(cutNum);
            }, 1000);
        }
    }
}];
export {registerCtrl};